package com.biostime.springdraw.modules.sd.service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.springdraw.common.config.Global;
import com.biostime.springdraw.common.service.CrudService;
import com.biostime.springdraw.common.utils.StringUtils;
import com.biostime.springdraw.common.wechat.BiostimeWechat;
import com.biostime.springdraw.modules.sd.dao.MemberDao;
import com.biostime.springdraw.modules.sd.entity.Member;
import com.biostime.springdraw.modules.sys.entity.User;

/**
 * 微信用户Service
 * @author king
 * @version 2016-01-18
 */
@Service
@Transactional
public class MemberService extends CrudService<MemberDao, Member> {

	public Member getByOpenId(String openId) {
		return dao.getByOpenId(openId);
	}
	
	public String insertMember(Map<String,String> ret ,String openId){
		String memberId = "0";
		Member member = getByOpenId(openId);
		if(member  == null){
			Member m = new Member();
			m.setOpenid(openId);
			m.setCustomerid(ret.get("customerId"));	
			m.setMobile(ret.get("mobile"));  
			m.setCreateBy(new User("0"));
			m.setCreateDate(new Date());
			m.setUpdateBy(new User("0"));
			m.setUpdateDate(new Date());			
			m.setNickname(ret.get("nickName"));  		
			dao.insert(m);			
			memberId = m.getId();
		}else{
			memberId = member.getId();
		}
		return memberId;
	}
		 
}
